/*
 * ThemeMenu.java  0.4.0 / Oct 23, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swingx;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.cute4j.swing.CheckBoxMenuItem;
import com.cute4j.swing.Menu;
import com.cute4j.swing.RadioButtonMenuItem;
import com.cute4j.swing.Swing;
import com.cute4j.swing.events.ActionEvent;
import com.cute4j.swing.events.ActionListener;
import com.cute4j.swing.styles.SystemStyle;
import com.cute4j.swing.styles.Theme;

/**
 * Theme Menu.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class ThemeMenu extends Menu {

// Package-Private Fields. -----------------------------------------------------

  boolean themeChangeEnabled;
  final ThemeChangeListener themeChangeListener;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new ThemeMenu object.
   */
  public ThemeMenu() {
    this("&Theme");
  }

  /**
   * Constructs new ThemeMenu object.
   *
   * @param title the menu title
   */
  public ThemeMenu(String title) {
    super(title);
    boolean useSystemTheme = Swing.isUsingSystemTheme();
    AlwaysUseSystemActionListener ausal =
                                       new AlwaysUseSystemActionListener(this);
    CheckBoxMenuItem cbmi = new CheckBoxMenuItem("&Always Use System");
    cbmi.setChecked(useSystemTheme);
    add(cbmi);
    cbmi.addActionListener(ausal);
    addSeparator();
    String currentThemeName = Swing.getThemeName();
    String[] themes = Swing.getInstalledThemeNames();
    ThemeActionListener themeActionListener = new ThemeActionListener(this);
    for (int i = 0; i < themes.length; i++) {
      String themeName = themes[i];
      RadioButtonMenuItem rbmi = new RadioButtonMenuItem(themeName);
      add(rbmi);
      if (themeName.equals(currentThemeName)) {
        rbmi.setSelected(true);
      }
      rbmi.addActionListener(themeActionListener);
    }
    themeChangeListener = new ThemeChangeListener(this);
    Swing.addPropertyChangeListener(Swing.PROPERTY_THEME, themeChangeListener);
    themeChangeEnabled = true;
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected void disposeComponent() {
    Swing.removePropertyChangeListener(Swing.PROPERTY_THEME,
                                       themeChangeListener);
    super.disposeComponent();
  }

// Inner Classes. --------------------------------------------------------------

  /**
   * Always system action listener.
   */
  static final class AlwaysUseSystemActionListener implements ActionListener {
    private final ThemeMenu themeMenu;
    public AlwaysUseSystemActionListener(ThemeMenu themeMenu) {
      this.themeMenu = themeMenu;
    }
    public void actionPerformed(ActionEvent e) {
      boolean alwaysUseSystem = ((CheckBoxMenuItem)e.getSource()).isChecked();
      Swing.setUseSystemTheme(alwaysUseSystem);
      alwaysUseSystem = !alwaysUseSystem;
      final ThemeMenu themeMenu = this.themeMenu;
      for (int i = themeMenu.getComponentCount(); --i > 1;) {
        themeMenu.getComponent(i).setEnabled(alwaysUseSystem);
      }
    }
  }

  /**
   * Theme action listener.
   */
  static final class ThemeActionListener implements ActionListener {
    private final ThemeMenu themeMenu;
    public ThemeActionListener(ThemeMenu themeMenu) {
      this.themeMenu = themeMenu;
    }
    public void actionPerformed(ActionEvent e) {
      if (themeMenu.themeChangeEnabled) {
        RadioButtonMenuItem rbmi = (RadioButtonMenuItem)e.getSource();
        if (rbmi.isSelected()) {
          Swing.setThemeSafely(rbmi.getText());
        }
      }
    }
  }

  /**
   * Theme property change listener.
   */
  static final class ThemeChangeListener implements PropertyChangeListener {
    private final ThemeMenu themeMenu;
    public ThemeChangeListener(ThemeMenu themeMenu) {
      this.themeMenu = themeMenu;
    }
    public void propertyChange(PropertyChangeEvent e) {
      final ThemeMenu themeMenu = this.themeMenu;
      themeMenu.themeChangeEnabled = false;
      String themeName = ((Theme)e.getNewValue()).getName();
      if (themeName.equals(SystemStyle.STYLE_NAME)) { // NOPMD
        themeName = "System";
      }
      for (int i = themeMenu.getComponentCount(); --i > 1;) {
        RadioButtonMenuItem rbmi =
                               (RadioButtonMenuItem)themeMenu.getComponent(i);
        if (themeName.equals(rbmi.getText())) {
          rbmi.setSelected(true);
          break;
        }
      }
      themeMenu.themeChangeEnabled = true;
    }
  }

}
